package com.dazhi.show.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dazhi.show.core.Result;
import com.dazhi.show.user.dao.User;
import com.dazhi.show.user.service.UserService;
import com.dazhi.show.user.vo.LoginVo;
import com.dazhi.show.user.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/login")
public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result<List<User>> list() {
        List<User> list = userService.list();
        return new Result<List<User>>(200, "success", list);
    }

    @PostMapping
    public Result<UserInfoVo> login(@RequestBody LoginVo loginVo){
        Result<UserInfoVo> res = new Result<>(200, "success");
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",loginVo.getUsername());
        User user = userService.getOne(queryWrapper);

        UserInfoVo userInfoVo = new UserInfoVo();
        System.out.println(loginVo.getUsername()+"===="+loginVo.getPass());
        if (user.getPass().equals(loginVo.getPass())) {
            userInfoVo.setUsername(loginVo.getUsername());
            userInfoVo.setToken("fasfdasfdasfdasfadsfafd");
            res.setData(userInfoVo);
        }
        return res;
    }
}
